package org.pettyfox.pfpay.controller.admin;

import org.codehaus.plexus.util.StringUtils;
import org.pettyfox.pfpay.biz.PayOrderBiz;
import org.pettyfox.pfpay.entity.PayOrder;
import org.pettyfox.pfpay.utils.BeanMapUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 支付审核页面
 */
@Controller
@RequestMapping("/admin/audit")
public class AuditRest {
    //https://oapi.dingtalk.com/robot/send?access_token=98b4514612e60e1fe169c41025fdff82d813e29f9369dc88c955325cf20efdaf
    public static String PAY_AUDIT_KEYS = "pay-audit-keys-";
    @Resource
    PayOrderBiz payOrderBiz;
    @Resource
    RedisTemplate redisTemplate;
    @GetMapping("{key}")
    public String audit(Model model,@PathVariable String key){
        if(StringUtils.isEmpty(key)){
            return "audit/404";
        }
        boolean valid = redisTemplate.hasKey(PAY_AUDIT_KEYS+key);
        if(!valid){
            return "audit/404"; //页面失效了
        }
        Long orderId = (Long) redisTemplate.opsForValue().get(PAY_AUDIT_KEYS+key);

        PayOrder payOrder = payOrderBiz.get(orderId);

        Map<String,Object> map = BeanMapUtil.objectToMap(payOrder);
        map.put("payType",payOrder.getPayType()==0?"微信":"支付宝");
        map.put("orderStatus",PayOrder.OrderStatus.get(payOrder.getOrderStatus()).name());
        model.addAllAttributes(map);

        return "audit/index";
    }
}
